package com.sxbbc.bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.bank.service.IBankLoanApplyRecordService;
import com.sxbbc.bank.service.IBankLoanService;
import com.sxbbc.bank.service.IBankMultiInformationService;
import com.sxbbc.bank.service.IBankSubBranchService;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.rest.RestResponse;
import com.sxbbc.common.core.entity.banks.BankLoanApplyRecord;
import com.sxbbc.common.core.mapper.banks.BankLoanApplyRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2020-09-02
 */
@Service
public class BankLoanApplyRecordServiceImpl extends ServiceImpl<BankLoanApplyRecordMapper, BankLoanApplyRecord> implements IBankLoanApplyRecordService {

    private static Logger logger = LogManager.getLogger(BankLoanApplyRecordServiceImpl.class);


    @Autowired
    private IBankMultiInformationService iBankMultiInformationService;

    @Autowired
    private IBankSubBranchService iBankSubBranchService;

    @Autowired
    private BankLoanApplyRecordMapper bankLoanApplyRecordMapper;

    @Autowired
    private IBankLoanService iBankLoanService;


    public  RestResponse listDate(BasePage<BankLoanApplyRecord> page, BankLoanApplyRecord bankLoanApplyRecord, String yyTime){
        QueryWrapper<BankLoanApplyRecord> wrapper = new QueryWrapper<>();
        if(null!=bankLoanApplyRecord){
            if(StringUtils.isNotEmpty(bankLoanApplyRecord.getFullName())){
                wrapper.like("full_name",bankLoanApplyRecord.getFullName());
            }
            if(StringUtils.isNotEmpty(bankLoanApplyRecord.getIdCard())){
                wrapper.like("id_card",bankLoanApplyRecord.getIdCard());
            }
            if(null!=bankLoanApplyRecord.getPhoneNumber()){
                wrapper.like("phone_number",bankLoanApplyRecord.getPhoneNumber());
            }
            if(null!=yyTime && yyTime.trim().length()>0){
                String [] arr=yyTime.split("~");
                wrapper.between("apply_time",arr[0],arr[1]);
            }
            if(null!=bankLoanApplyRecord.getHandleResult() ){
                wrapper.eq("handle_result",bankLoanApplyRecord.getHandleResult());
                if(bankLoanApplyRecord.getHandleResult()==3){
                    wrapper.orderByDesc("apply_time");
                }else {
                    wrapper.orderByDesc("last_operate_time");
                }
            }else{
                wrapper.eq("handle_result",3);
                wrapper.orderByDesc("apply_time");
            }
            if(null!=bankLoanApplyRecord.getProductTypeId() && bankLoanApplyRecord.getProductTypeId().toString().trim().length()>0){
                wrapper.eq("product_type_id",bankLoanApplyRecord.getProductTypeId());
            }

        }

        super.page(page,wrapper);
        Map<String,String> loadMap=iBankLoanService.getDataForMap();
        for (int i = 0; i < page.getRecords().size(); i++) {
            BankLoanApplyRecord bank=page.getRecords().get(i);
           page.getRecords().get(i).setProductTypeId(loadMap.get(bank.getProductTypeId()));
        }
        return RestFactory.success(page.getRecords(), page.getTotal());
    };


    public int updateHandleResult(Integer id,Integer status){
        BankLoanApplyRecord bankLoanApplyRecord=super.getById(id);
        bankLoanApplyRecord.setHandleResult(status);
        bankLoanApplyRecord.setLastOperateTime(new Date());
        super.updateById(bankLoanApplyRecord);
        return  0;
    }

    public BankLoanApplyRecord getById(String id){
        BankLoanApplyRecord bankLoanApplyRecord= super.getById(id);
        //证件类型
        Map<String,String> sfzMap=iBankMultiInformationService.listMap(0);
        //支行
        Map<String,String> zhMap=iBankSubBranchService.map();
        bankLoanApplyRecord.setCardTypeId(sfzMap.get(bankLoanApplyRecord.getCardTypeId()));
        bankLoanApplyRecord.setSubBranchId(zhMap.get(bankLoanApplyRecord.getSubBranchId()));
        return bankLoanApplyRecord;
    }
}
